package com.navinfo.platform.fault.utils.day;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.navinfo.location.mileage.util.JsonUtil;
import com.navinfo.platform.fault.dto.*;
import com.navinfo.platform.common.utils.DataUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.spark.sql.Row;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import scala.collection.mutable.WrappedArray;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Function;

public class VehicleBasicDataUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(VehicleBasicDataUtil.class);
    public static BasicDataDto parseRowToBasicDataDto(Row row) {
        BasicDataDto basicDataDto = new BasicDataDto();

        basicDataDto.setTid(row.getAs("tid"));







        return basicDataDto;
    }

    /**
     * 计算吨/公里
     */
    private static void calcTonKM(Row row, BasicDataDto basicDataDto) {
        WrappedArray.ofRef<Integer> mileage;
        WrappedArray.ofRef<Integer> load;

        try {
            mileage = row.getAs("trip_mileage_list");
            load = row.getAs("trip_load_list");
        }catch (UnsupportedOperationException|IllegalArgumentException e){
            LOGGER.error("计算计算吨/公里失败......", e);
            return;
        }


    }

    /**
     * 计算经过城市的详情信息
     */
    private static void calcCrossCitiesInfo(Row row, BasicDataDto basicDataDto) {
        WrappedArray.ofRef<String> cross_cities_list;
        try {
            cross_cities_list = row.getAs("cross_cities_list");
        }catch (UnsupportedOperationException|IllegalArgumentException e){
            LOGGER.error("计算经过城市的详情信息失败......", e);
            return;
        }


    }




    /***
     * 当日行程里程之和，单位：m，
     * 1、行程时间大于等于5分钟,行程距离大于等于1千米,
     *    百公里油耗大于0小于80,总油耗大于0''
     * @param row
     * @param basicDataDto
     */
    private static void calcTripMileageTotal(Row row, BasicDataDto basicDataDto) {
        WrappedArray.ofRef<Integer> mileage;
        WrappedArray.ofRef<Integer> fuel;
        WrappedArray.ofRef<Integer> duration;
        WrappedArray.ofRef<Integer> cross_day_flag;
        Long tripMileageTotal = 0L;
        int fuel_100km = 0;
        try{
            mileage = row.getAs("trip_mileage_list");
            fuel = row.getAs("trip_fuel_list");
            duration = row.getAs("trip_duration_list");
            cross_day_flag = row.getAs("cross_day_flag_list");
        }catch (UnsupportedOperationException|IllegalArgumentException e){
            LOGGER.error("计算当日行程里程和......", e);
            return;
        }
        for(int i=0; i<mileage.size(); i++) {
            fuel_100km = DataUtil.remainTwoBit(fuel.apply(i),mileage.apply(i));

            if(mileage.apply(i)>1000 && duration.apply(i)>300 && fuel.apply(i)>0 && fuel_100km>0 && fuel_100km <8000 && cross_day_flag.apply(i) == 0 ){
                tripMileageTotal = tripMileageTotal + mileage.apply(i);
            }
        }

    }

}
